﻿using BaseDal;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseBll
{
    public class RoleBll
    {
        static readonly Lazy<RoleBll> dal = new Lazy<RoleBll>(() => new RoleBll());
        public static RoleBll Instance { get; } = dal.Value;
        private RoleBll() { }

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return RoleDal.Instance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataTable GetPageList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return RoleDal.Instance.GetPageList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Role> GetList(string strWhere)
        {
            return RoleDal.Instance.GetList(strWhere);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Role GetModelById(int id)
        {
            return RoleDal.Instance.GetModelById(id);
        }

        /// <summary>
        /// 增加一条用户角色数据
        /// </summary>
        public bool AddRole(Role model)
        {
            return RoleDal.Instance.AddRole(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateRole(Role model)
        {
            return RoleDal.Instance.UpdateRole(model);
        }

        /// <summary>
        /// 删除一条数据，及子表所有相关数据
        /// </summary>
        public bool DeleteRole(int roleId, string userName)
        {
            return RoleDal.Instance.DeleteRole(roleId, userName);
        }

        /// <summary>
        /// 修改状态（启用、禁用）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int UpdateStatus(int id, int status, string userName)
        {
            return RoleDal.Instance.UpdateStatus(id, status, userName);
        }

        /// <summary>
        /// 获取角色菜单权限
        /// </summary>
        /// <param name="roleType"></param>
        /// <param name="menus"></param>
        /// <returns></returns>
        public List<RoleValue> GetRoleValues(int isAdmain, string menus)
        {
            List<RoleValue> list = new List<RoleValue>();
            //判断角色类型
            if (isAdmain == 1)
            {
                //如果为超级管理员，拥有所有权限
                list = null;
            }
            else
            {
                //JArray jarray = JArray.Parse(menus);
                //for (int i = 0; i < jarray.Count; i++)
                //{
                //    JObject obj = (JObject)jarray[i];
                //    list.Add(new RoleValue
                //    {
                //        MenuId = obj["menuId"].ToString(),
                //        typeNumber = obj["action"].ToString(),
                //    });
                //}
                string[] arr = menus.Split(',');
                for (int i = 0; i < arr.Length; i++)
                {
                    list.Add(new RoleValue
                    {
                        MenuId = int.Parse(arr[i])
                    });
                }
            }
            return list;
        }

        public bool HasUsed(int roleId)
        {
            return UserBll.Instance.IsExist(" and RoleId=" + roleId);
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel(Role model, ref string msg)
        {

            if (string.IsNullOrEmpty(model.RoleName))
            {
                msg = "角色名称不能为空";
                return false;
            }

            return true;
        }

    }
}
